/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.engine.core.action.query;

import com.inspur.edp.bff.api.manager.context.QueryContext;
import com.inspur.edp.bff.engine.core.action.filter.EngineQueryConditionAction;
import com.inspur.edp.bff.spi.action.query.AbstractQueryAction;
import com.inspur.edp.bff.spi.action.query.AfterQueryAction;
import com.inspur.edp.bff.spi.action.query.BeforeQueryAction;
import com.inspur.edp.bff.spi.action.query.DefaultQueryAction;
import com.inspur.edp.bff.spi.assembler.AbstractExtendQueryAssembler;
import com.inspur.edp.cef.spi.jsonser.base.StringUtils;
import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.formserver.viewmodel.collection.VMActionCollection;
import com.inspur.edp.formserver.viewmodel.common.InternalExtendActionUtil;

import java.util.ArrayList;

import static com.inspur.edp.bff.engine.core.action.BffEngineAssemblerBase.getActions;

public class BffEngineQueryAssembler extends AbstractExtendQueryAssembler {

    private GspViewModel vo;


    public BffEngineQueryAssembler(GspViewModel vo) {
        this.vo = vo;
    }


    private ArrayList<Class>  getParamTypes(){
        ArrayList<Class> paramTypes = new ArrayList<>();
        paramTypes.add(QueryContext.class);
        return paramTypes;
    }

    private ArrayList<Object> getParamValues(QueryContext queryContext){
        ArrayList<Object> values = new ArrayList<>();
        values.add(queryContext);
        return values;
    }
    @Override
    protected ArrayList<BeforeQueryAction> getBeforeQueryActions(QueryContext queryContext) {

        ArrayList<BeforeQueryAction> beforeQueryAction = new ArrayList<>();
        if(!StringUtils.isNullOrEmpty(vo.getMainObject().getFilterCondition())
                || !StringUtils.isNullOrEmpty(vo.getMainObject().getOrderbyCondition())){
            beforeQueryAction.add(new EngineQueryConditionAction(vo.getMainObject(), queryContext));
        }

        VMActionCollection actions = vo.getDataExtendInfo().getBeforeQueryActions();
        ArrayList<BeforeQueryAction> extendActions = getActions(BeforeQueryAction.class, actions, getParamTypes(), getParamValues(queryContext),null, null);
        if(extendActions != null)
            beforeQueryAction.addAll(extendActions);
        return beforeQueryAction;
    }

    @Override
    protected ArrayList<AbstractQueryAction> getQueryActions(QueryContext queryContext) {

        VMActionCollection actions = vo.getDataExtendInfo().getQueryActions();
        return getActions(
                AbstractQueryAction.class,
                actions,
                getParamTypes(),
                getParamValues(queryContext),
                new DefaultQueryAction(queryContext),
                InternalExtendActionUtil.QueryActionId);
    }

    @Override
    protected ArrayList<AfterQueryAction> getAfterQueryActions(QueryContext queryContext) {
        VMActionCollection actions = vo.getDataExtendInfo().getAfterQueryActions();
        return getActions(AfterQueryAction.class, actions, getParamTypes(), getParamValues(queryContext), null, null);
    }

}
